#include <tsin/library/BulletSectorPlacer.hpp>

#include <tsin/runtime/BulletRuntime.hpp>
#include <tsin/util/Logger.hpp>

#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace tsin;

void BulletSectorPlacer::init(float centerX, float centerY, float radius, int n, float centerAngle, float angleRange)
{

        this->x = centerX;
        this->y = centerY;
        this->radius = radius;
        if (n == 1) {
                this->start = centerAngle;
                this->interval = 0.0f;
        } else {
                float fn = static_cast<float>(n);
                this->start = centerAngle - angleRange / 2;
                this->interval = angleRange / (fn - 1);
        }
}

void BulletSectorPlacer::reinit()
{
        float arg1 = arguments[0]->getFloat();
        float arg2 = arguments[1]->getFloat();
        float arg3 = arguments[2]->getFloat();
        int   arg4 = arguments[3]->getFloat();
        float arg5 = arguments[4]->getFloatInRad();
        float arg6 = arguments[5]->getFloatInRad();

        switch(nrArguments) {
        case 6:
                if (strcmp(desp, "NNNNNN") == 0)
                        init(arg1, arg2, arg3, arg4, arg5, arg6);
                break;
        }
}

void BulletSectorPlacer::reset()
{
        count = 0;
}

void BulletSectorPlacer::place(BulletRuntime *bullet, void*)
{
        float angle = start + interval * count;
        bullet->x = x - radius * sin(angle);
        bullet->y = y + radius * cos(angle);
        bullet->angle = angle;
        count ++;
}

DynamicObject *bindSectorPlacer()
{
        return new BulletSectorPlacer;
}
